<?php
/**
 * Created by cmb_weixin.master.
 * File: User.php
 * User: xuewl
 * Date: 2017/12/11
 * Time: 22:53
 */

namespace app\user\model;
use app\common\library\Model;

class User extends Model
{
    protected $gender = ['女', '男'];
    protected $rules = ['普通用户', '分销商', '客户经理'];

    protected $append = [
        'gender_name','role_name'
    ];

    public function getRoleNameAttr($value, $data) {
        return $this->rules[intval($data['role'])];
    }

    public function getGenderNameAttr($value, $data) {
        return $this->gender[intval($data['gender'])];
    }

    public function distributor() {
        return $this->hasOne('\\app\\user\\model\\User', "id", "distributor_id");
    }

    public function customer() {
        return $this->hasOne('\\app\\user\\model\\User', "id", "customer_id");
    }


    public function getWechatFansAttr($value, $data) {
        return \think\Db::name('wechat_fans')
            ->where([
                'unionid' => $data['unionid']
            ])
            ->find();
    }

    // 统计
    public function getProductOrderTotalAttr($value, $data) {
        if($data['role'] == 2) {
            return \think\Db::name('product_order')->where([
                'customer_id' => $data['id']
            ])->count();
        }
        return 0;
    }
}